package com.dk.controller;

import com.dk.pojo.Publisher;
import com.dk.pojo.TAuthor;
import com.dk.pojo.TBook;
import com.dk.pojo.TCategory;
import com.dk.service.AuthorService;
import com.dk.service.BookService;
import com.dk.service.CategoryService;
import com.dk.service.PublisherService;
import com.dk.vo.BookVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("book")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private AuthorService authorService;

    @Autowired
    private PublisherService publisherService;

    @Autowired
    private CategoryService categoryService;

    public void getList(Model model){
        List<TCategory> list = categoryService.selectCat();
        model.addAttribute("list",list);
        List<TAuthor> tAuthorList = authorService.selectAuthor();
        model.addAttribute("alist",tAuthorList);
        List<Publisher> publisherList = publisherService.selectPublishers();
        model.addAttribute("plist",publisherList);
    }

    @RequestMapping("toList")
    public String toList(Model model){
        /*List<TCategory> list = categoryService.selectCat();
        model.addAttribute("list",list);
        List<TAuthor> tAuthorList = authorService.selectAuthor();
        model.addAttribute("alist",tAuthorList);
        List<Publisher> publisherList = publisherService.selectPublishers();
        model.addAttribute("plist",publisherList);*/
        getList(model);
        return "book/list";
    }
    @RequestMapping("selectBook")
    @ResponseBody
    public Map<String,Object> selectBook(TBook tbook, @RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "5")Integer pageSize){
        PageInfo<BookVo> page = bookService.selectUser(tbook,pageNum,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("total",page.getTotal());
        map.put("rows",page.getList());
        return map;
    }
    @RequestMapping("toAddBook")
    public String toAddBook(Model model){
        /*List<TCategory> list = categoryService.selectCat();
        model.addAttribute("list",list);
        List<TAuthor> tAuthorList = authorService.selectAuthor();
        model.addAttribute("alist",tAuthorList);
        List<Publisher> publisherList = publisherService.selectPublishers();
        model.addAttribute("plist",publisherList);*/
        getList(model);
        return "book/add";
    }

    @RequestMapping("addBook")
    @ResponseBody
    public Boolean addBook(TBook tBook){
        tBook.setBookState(0);
        bookService.addBook(tBook);
        return true;
    }
    @RequestMapping("toUpdateBook/{bookId}")
    public String toUpdateBook(@PathVariable("bookId")Integer bookId,Model model){
        TBook tBook = bookService.toUpdateBook(bookId);
        model.addAttribute("t",tBook);
        /*TBook tBook = bookService.toUpdateBook(bookId);
        model.addAttribute("t",tBook);
        List<TCategory> list = categoryService.selectCat();
        model.addAttribute("list",list);
        List<TAuthor> tAuthorList = authorService.selectAuthor();
        model.addAttribute("alist",tAuthorList);
        List<Publisher> publisherList = publisherService.selectPublishers();
        model.addAttribute("plist",publisherList);*/
        getList(model);
        return "book/update";
    }
    @RequestMapping("updateBook")
    @ResponseBody
    public Boolean updateBook(TBook tBook){
        bookService.updateBook(tBook);
        return true;
    }
    @RequestMapping("deleteBook/{bookId}")
    @ResponseBody
    public Boolean deleteBook(@PathVariable("bookId")Integer bookId){
        bookService.deleteBook(bookId);
        return true;
    }
    @RequestMapping("deleteBookAll")
    @ResponseBody
    public Boolean deleteBookAll(@RequestParam("ids[]")Integer[] ids){
        bookService.deleteBookAll(ids);
        return true;
    }
    //下架
    @RequestMapping("updateTrue/{bookId}")
    @ResponseBody
    public boolean updateTrue(@PathVariable("bookId") Integer bookId) {
        TBook article = null;
        int i = 0;
        try {
            article = bookService.getInfoById(bookId);
            article.setBookState(0);
            bookService.update(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i > 0;
    }

    //上架
    @RequestMapping("updateFalse/{bookId}")
    @ResponseBody
    public boolean updateFalse(@PathVariable("bookId") Integer bookId) {
        TBook article = null;
        int i = 0;
        try {
            article = bookService.getInfoById(bookId);
            article.setBookState(1);
            bookService.update(article);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i > 0;
    }
}
